#ifndef ACCOUNT_HH
#define ACCOUNT_HH

#include <string>
#include <vector>
#include <memory>
#include <fstream>
#include "Transaction.hh"
#include "Utils.hh"
#include <Windows.h>

namespace BankSystem {
    // 账户接口（抽象基类）
    
    class Account {
    protected:
        std::string accountId;
        std::string customerName;
        double balance;
        std::vector<std::shared_ptr<Transaction>> transactions;
        
    public:
        Account(const std::string& id, const std::string& name, double initialBalance = 0.0)
            : accountId(id), customerName(name), balance(initialBalance) {}
        
        virtual ~Account() = default;
        // Getters
        std::string getAccountId() const { return accountId; }
        std::string getCustomerName() const { return customerName; }
        double getBalance() const { return balance; }
        virtual void receiveTransfer(double amount)=0;
        // 返回账户类型的虚函数
        virtual std::string getAccountType() const = 0;
        
        // 添加交易记录
        void addTransaction(const std::shared_ptr<Transaction>& transaction) {
            transactions.push_back(transaction);
        }
        
        // 获取所有交易记录
        const std::vector<std::shared_ptr<Transaction>>& getTransactions() const {
            return transactions;
        }
        
        // 转账操作（纯虚函数）
        virtual bool transfer(std::shared_ptr<Account> destination, double amount, const std::string& note = "") = 0;
        
        // 生成账户报告
        bool generateReport() const {
            if (transactions.empty()) {
                std::cout << "No transactions to report for this account." << std::endl;
                return false;
            }
            
            std::string filename = "transactions_" + accountId + "_" + Utils::getCurrentDate() + ".txt";
            std::ofstream reportFile(filename);
            
            if (!reportFile.is_open()) {
                std::cout << "Failed to create report file." << std::endl;
                return false;
            }
            
            reportFile << "====== Transaction Report for Account: " << accountId << " ======\n";
            reportFile << "Account Type: " << getAccountType() << "\n";
            reportFile << "Account Holder: " << customerName << "\n";
            reportFile << "Current Balance: " << Utils::formatCurrency(balance) << "\n";
            reportFile << "Transaction History:\n\n";
            
            for (const auto& transaction : transactions) {
                reportFile << "--------------------------------------\n";
                reportFile << transaction->getDetails() << "\n";
            }
            
            reportFile.close();
            
            std::cout << "Report successfully generated as \"" << filename << "\"" << std::endl;
            return true;
        }
    };
    
    // 储蓄账户类
    class SavingsAccount : public Account {
    public:
        SavingsAccount(const std::string& id, const std::string& name, double initialBalance = 0.0)
            : Account(id, name, initialBalance) {}
        
        std::string getAccountType() const override {
            return "Savings Account";
        }
       
        // 储蓄账户只能转账到同一客户的支票账户
        bool transfer(std::shared_ptr<Account> destination, double amount, const std::string& note = "") override {
            // 检查目标账户是否为支票账户且属于同一客户
            if (destination->getAccountType() != "Checking Account" || 
                destination->getCustomerName() != customerName) {
                std::cout << "Error: Savings accounts can only transfer to the same customer's checking account." << std::endl;
                return false;
            }
            
            // 检查余额是否充足
            if (balance < amount) {
                std::cout << "Error: Insufficient funds for transfer." << std::endl;
                return false;
            }
            
            // 执行转账
            balance -= amount;
            destination->receiveTransfer(amount);
            
            // 创建并记录交易
            auto transaction = std::make_shared<Transaction>(
                accountId, destination->getAccountId(), amount, note
            );
            addTransaction(transaction);
            destination->addTransaction(transaction);
            
            return true;
        }
        
        

        // 接收转账金额（内部使用）
        void receiveTransfer(double amount) {
            balance += amount;
        }
    };
    
    // 支票账户类
    class CheckingAccount : public Account {
    public:
        CheckingAccount(const std::string& id, const std::string& name, double initialBalance = 0.0)
            : Account(id, name, initialBalance) {}
        
        std::string getAccountType() const override {
            return "Checking Account";
        }
        
        // 支票账户可以转账到任何账户
        bool transfer(std::shared_ptr<Account> destination, double amount, const std::string& note = "") override {
            // 检查余额是否充足
            if (balance < amount) {
                std::cout << "Error: Insufficient funds for transfer." << std::endl;
                return false;
            }
            
            // 执行转账
            balance -= amount;
            destination->receiveTransfer(amount);
            
            // 创建并记录交易
            auto transaction = std::make_shared<Transaction>(
                accountId, destination->getAccountId(), amount, note
            );
            addTransaction(transaction);
            destination->addTransaction(transaction);
            
            return true;
        }
        
        // 存款操作
        bool deposit(double amount, const std::string& note = "") {
            if (amount <= 0) {
                std::cout << "Error: Deposit amount must be positive." << std::endl;
                return false;
            }
            
            balance += amount;
            
            // 创建并记录交易
            auto transaction = std::make_shared<Transaction>(
                TransactionType::DEPOSIT, accountId, amount, note
            );
            addTransaction(transaction);
            
            return true;
        }
        
        // 取款操作
        bool withdraw(double amount, const std::string& note = "") {
            if (amount <= 0) {
                std::cout << "Error: Withdrawal amount must be positive." << std::endl;
                return false;
            }
            
            if (balance < amount) {
                std::cout << "Error: Insufficient funds for withdrawal." << std::endl;
                return false;
            }
            
            balance -= amount;
            
            // 创建并记录交易
            auto transaction = std::make_shared<Transaction>(
                TransactionType::WITHDRAW, accountId, amount, note
            );
            addTransaction(transaction);
            
            return true;
        }
        
      
        // 接收转账金额（内部使用）
        void receiveTransfer(double amount) {
            balance += amount;
        }
    };
}

#endif // ACCOUNT_H